package com.neusoft.chenanqi.dao;

import java.util.List;

import javax.inject.Inject;
import javax.validation.constraints.Null;

import org.springframework.stereotype.Repository;

import com.neusoft.chenanqi.dto.EmpSalDto;
import com.zero.basic.dao.DefaultBaseDao;
import com.zero.basic.dao.SqlListBaseDao;
import com.zero.basic.dao.SqlPagingListBaseDao;
import com.zero.basic.dao.SqlQueryBaseDao;
import com.zero.basic.exception.SmsDaoException;
import com.zero.basic.model.Pager;
import com.zero.mms.model.EmpSalary;

@Repository("empSalaryDao")
public class EmpSalaryDaoImpl extends DefaultBaseDao<EmpSalary> implements
		EmpSalaryDao {

	@Inject
	private SqlPagingListBaseDao<?> sqlPagingListBaseDao;
	
	@Inject
	private SqlQueryBaseDao<?> sqlQueryBaseDao;

	@Override
	public Pager<EmpSalDto> listEmpSal(String depName, String empNo,
			String month, Double salFrom, Double salTo, String type) throws SmsDaoException {
		try {
			String sql = "select d.dep_name as depName, e.emp_id as empNo, "
					+ "e.emp_name as empName, s.emp_salary_date as month "
					+ "from t_emp_salary s, t_emp e, t_dep d, t_salary_item i "
					+ "where s.emp_id = e.emp_id and s.salary_item_id = i.salary_item_id "
					+ "and e.dep_id = d.dep_id and i.salary_item_type = '"
					+ type + "'";

			if (!"*".equals(depName)) {
				sql += " and d.dep_name like '%" + depName + "%'";
			}
			if (!"*".equals(empNo)) {
				sql += " and e.emp_id like '%" + empNo + "%'";
			}
			if (!"*".equals(month)) {
				sql += " and s.emp_salary_date like '%" + month + "%'";
			}
			if (salFrom != null) {
				sql += " and s.emp_salary_num >= ?";
			}
			if (salTo != null) {
				sql += " and s.emp_salary_num <= ?";
			}
			sql += " group by e.emp_id, s.emp_salary_date";
			System.out.println(sql);
			Pager<EmpSalDto> empSalDtoList = sqlPagingListBaseDao.findBySql(sql, new Object[]{salFrom, salTo}, EmpSalDto.class, false);
			return empSalDtoList;
		} catch (Exception e) {
			throw new SmsDaoException();
		}
	}

	@Override
	public EmpSalary getSalItemValueByEmpNoMonthAndItemName(Integer empNo,
			String month, String itemName) throws SmsDaoException {
		try {
			String sql = "select * from t_emp_salary s, t_salary_item i "
					+ "where s.salary_item_id = i.salary_item_id and s.emp_id = '" + empNo
					+ "' and s.emp_salary_date = '" + month
					+ "' and i.salary_item_name = '" + itemName + "'";
			EmpSalary empSalary = (EmpSalary) sqlQueryBaseDao.queryBySql(sql, null, null, EmpSalary.class, true, true);
			return empSalary;
		} catch (Exception e) {
			throw new SmsDaoException();
		}
	}
}
